﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Membership.Security.OAuth.AuthorizationServer
{
    public  class DynamicPasswordMessage
    {
        /// <summary>
        /// 短信帐号
        /// </summary>
        static String username = "JSM40576";

        /// <summary>
        /// 登录密码
        /// </summary>
        static String password = "cvxrr216";

        /// <summary>
        /// 校验码
        /// </summary>
        static String veryCode = "org6rm34d1qi";

        /// <summary>
        /// 短信接口URL 
        /// </summary>
        const String HTTP_URL = "http://112.74.76.186:8030/service/httpService/httpInterface.do?method=";
        /// <summary>
        /// 发送模版短信(短信模版的创建参考客户端操作手册)
        /// 
        /// 模版：@1@会员，动态验证码@2@(五分钟内有效)。请注意保密，勿将验证码告知他人。
        /// 参数值:@1@=member,@2@=4293
        /// 最终短信内容：【短信签名】member会员，动态验证码4293(五分钟内有效)。请注意保密，勿将验证码告知他人。
        /// 
        /// </summary>
        /// <param name="mobile">接收人手机号</param>
        /// <param name="tempId">模版编号</param>
        /// <param name="content">各参数值，以英文逗号隔开，如：@1@=member,@2@=4293</param>
        /// <returns>返回发送状态</returns>
        public static String SendTemplateSms(String mobile, String tempId, String content)
        {
            StringBuilder param = new StringBuilder();
            param.Append("&username=").Append(username);
            param.Append("&password=").Append(password);
            param.Append("&veryCode=").Append(veryCode);
            param.Append("&mobile=").Append(mobile);
            param.Append("&content=").Append(content);
            param.Append("&msgtype=").Append("2");
            param.Append("&tempid=").Append(tempId);
            param.Append("&code=").Append("utf-8");

            String address = String.Format("{0}sendMsg", HTTP_URL);

            //发送短信，并获取发送状态
            String result = HTTPHelper.Post(address, param.ToString());

            return result;
        }
    }

    public class HTTPHelper
    {

        public HTTPHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        /// <summary>
        /// POST请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string Post(string url, string paramData)
        {
            Stream respStream = null;
            HttpWebResponse httpResp = null;
            StreamReader respStreamReader = null;

            string res = null;
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
                HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
                httpReq.Method = "POST";
                httpReq.ContentType = "application/x-www-form-urlencoded";
                httpReq.ContentLength = byteArray.Length;
                respStream = httpReq.GetRequestStream();
                respStream.Write(byteArray, 0, byteArray.Length);//写入参数
                respStream.Close();
                httpResp = (HttpWebResponse)httpReq.GetResponse();
                respStreamReader = new StreamReader(httpResp.GetResponseStream(), Encoding.UTF8);
                res = respStreamReader.ReadToEnd();

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if (respStream != null)
                {
                    respStream.Close();
                }

                if (httpResp != null)
                {
                    httpResp.Close();
                }

                if (respStreamReader != null)
                {
                    respStreamReader.Close();
                }
            }
            return res;
        }


    }
}
